package com.heima.test;

import com.heima.mapper.BrandMapper;
import com.heima.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MybatisTest {

    /**
     * 1.查询所有品牌信息
     */
    @Test
    public void selectAll() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll();
        System.out.println(brands);
        sqlSession.close();
    }

    @Test
    public void testSelectById() throws IOException {
        //接收参数，该id以后需要传递过来
        int id = 1;

        //1. 获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3. 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4. 执行方法
        Brand brand = brandMapper.selectById(id);
        System.out.println(brand);

        //5. 释放资源
        sqlSession.close();
    }



    /**
     * 3.条件查询;根据状态、公司名称、品牌名称来查询
     */
    @Test
    public void selectByCondition() throws Exception {
        int status=1;
        String companyName="华为";
        String brandName="华为";
        companyName="%"+companyName+"%";
        brandName="%"+brandName+"%";

        //创建Brand对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //方式一 ：接口方法参数使用 @Param 方式调用的方法
        //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
        //方式二 ：接口方法参数是 实体类对象 方式调用的方法
        //封装对象
        Brand brand = new Brand();
//        brand.setStatus(status);
//        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        List<Brand> brands = mapper.selectByConditionSingle(brand);

        //方式三：使用map集合包装参数
        Map map=new HashMap();
//        map.put("status",status);
//        map.put("companyName",companyName);
//        map.put("brandName",brandName);
//        List<Brand> brands = mapper.selectByCondition(map);


        System.out.println(brands);
        sqlSession.close();
    }

    /**
     * 3.条件查询;根据状态、公司名称、品牌名称来查询（动态查询）
     */
    @Test
    public void selectByConditionSQL() throws Exception {
        int status=1;
        String companyName="华为";
        String brandName="华为";
        companyName="%"+companyName+"%";
        brandName="%"+brandName+"%";

        //创建Brand对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //方式一 ：接口方法参数使用 @Param 方式调用的方法
        //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
        //方式二 ：接口方法参数是 实体类对象 方式调用的方法
        //封装对象
        Brand brand = new Brand();
//        brand.setStatus(status);
//        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        List<Brand> brands = mapper.selectByConditionSingle(brand);

        //方式三：使用map集合包装参数
        Map map=new HashMap();
//        map.put("status",status);
//        map.put("companyName",companyName);
//        map.put("brandName",brandName);
//        List<Brand> brands = mapper.selectByCondition(map);


        System.out.println(brands);
        sqlSession.close();
    }


    /**
     * 3.单个条件动态查询
     */
    @Test
    public void selectByConditionSingle() throws Exception {
        int status=1;
        String companyName="华为";
        String brandName="华为";
        companyName="%"+companyName+"%";
        brandName="%"+brandName+"%";

        //创建Brand对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //方式一 ：接口方法参数使用 @Param 方式调用的方法
        //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
        //方式二 ：接口方法参数是 实体类对象 方式调用的方法
        //封装对象
//        Brand brand = new Brand();
////        brand.setStatus(status);
////        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
//        List<Brand> brands = mapper.selectByConditionSingle(brand);

        //方式三：使用map集合包装参数
        Map map=new HashMap();
        map.put("status",status);
        map.put("companyName",companyName);
        map.put("brandName",brandName);
        List<Brand> brands = mapper.selectByCondition(map);


        System.out.println(brands);
        sqlSession.close();
    }


    /**
     * 3.添加方法
     */
    @Test
    public void add() throws Exception {
        int status=1;
        String companyName="华为";
        String brandName="华为";
        int ordered=100;
        String description="华为手机很好用";


//        companyName="%"+companyName+"%";
//        brandName="%"+brandName+"%";

        //创建Brand对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //设置mybatis自动提交事务
//        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //方式一 ：接口方法参数使用 @Param 方式调用的方法
        //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
        //方式二 ：接口方法参数是 实体类对象 方式调用的方法
        //封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(ordered);
        brand.setDescription(description);
        mapper.add(brand);

        //方式三：使用map集合包装参数
//        Map map=new HashMap();
//        map.put("status",status);
//        map.put("companyName",companyName);
//        map.put("brandName",brandName);
//        List<Brand> brands = mapper.selectByCondition(map);
        //提交事务
//        sqlSession.commit();
        System.out.println(brand.getId());
        sqlSession.close();
    }


    @Test
    /**
     * 更新值
     */
    public void update() throws Exception {
        int id=3;
        int status=0;
        String companyName="苹果";
        String brandName="苹果";
        int ordered=200;
        String description="苹果手机很好用";


//        companyName="%"+companyName+"%";
//        brandName="%"+brandName+"%";

        //创建Brand对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //设置mybatis自动提交事务
//        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //方式一 ：接口方法参数使用 @Param 方式调用的方法
        //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
        //方式二 ：接口方法参数是 实体类对象 方式调用的方法
        //封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setId(id);
//        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
//        brand.setOrdered(ordered);
//        brand.setDescription(description);
        int update = mapper.update(brand);
        if (update>0){
            System.out.println("更新成功");
        }else{
            System.out.println("修改失败");
        }

        //方式三：使用map集合包装参数
//        Map map=new HashMap();
//        map.put("status",status);
//        map.put("companyName",companyName);
//        map.put("brandName",brandName);
//        List<Brand> brands = mapper.selectByCondition(map);
        //提交事务
//        sqlSession.commit();
        sqlSession.close();
    }


    /**
     * 删除一个数据和批量删除数据
     * @throws Exception
     */
    @Test
    public void deleteById() throws Exception {
        int id=3;
        int status=0;
        String companyName="苹果";
        String brandName="苹果";
        int ordered=200;
        String description="苹果手机很好用";


//        companyName="%"+companyName+"%";
//        brandName="%"+brandName+"%";

        //创建Brand对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //设置mybatis自动提交事务
//        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //方式一 ：接口方法参数使用 @Param 方式调用的方法
        //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
        //方式二 ：接口方法参数是 实体类对象 方式调用的方法
        //封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setId(id);
//        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
//        brand.setOrdered(ordered);
//        brand.setDescription(description);
//        mapper.deleteById(8);
        int []ids={5,6,7};
        mapper.deleteByIds(ids);


        sqlSession.close();
    }

}
